import time
import pyautogui
import pyperclip
import src.caseinfo as caseinfo
import src.fileinfo as fileinfo
import src.send as send
import src.util.nacos.nacos_client as nacos_client   # 导入nacos模块init
from datetime import datetime
import json
import re
from selenium.webdriver.common.by import By

# 获取当前时间
current_time = datetime.now()

# 格式化时间为 YYYY-MM-DD 格式
formatted_time = current_time.strftime("%Y-%m-%d")
# 设置时间查询列表
def choice(driver):
    time.sleep(2)
    # 点击已提交归档
    pyautogui.click(148, 366)
    time.sleep(1)
    # 点击提交归档日期开始
    pyautogui.click(1605, 420)
    time.sleep(1)
    pyperclip.copy(formatted_time)
    pyautogui.hotkey('ctrl', 'v')
    # 点击提交归档日期结束
    pyautogui.click(1778, 422)
    pyperclip.copy(formatted_time)
    pyautogui.hotkey('ctrl', 'v')
    time.sleep(1)
    pyautogui.hotkey('enter')
    
    # 点击查询 并获取最后一个请求
    list_data=request(driver)
    time.sleep(3)
    # 新增查询条件
    inquire(list_data)
    case_info(driver)
# 点击卷宗信息、获取数据
def case_info(driver):
    #切换选项卡
    #driver.switch_to.window(driver.window_handles[-1])
    # 获取案件信息
    caseinfo1=caseinfo.get_case_info(driver)
    time.sleep(2)
    #点击卷宗信息
    pyautogui.click(1832, 205)
    time.sleep(2)
    # 登录
    islogin(driver)
    # 获取文件信息
    fileinfo1=fileinfo.init(driver)
    #发送数据
    send.send_data(caseinfo1,fileinfo1,driver)
# 登录
def islogin(driver):
    try:
        #切换选项卡
        time.sleep(3)
        driver.switch_to.window(driver.window_handles[-1])
        iframe= driver.find_element(By.ID,'iframe')
        iframe_src=iframe.get_attribute('src')
        pyautogui.hotkey('ctrl', 'l')
        time.sleep(1) 
        pyperclip.copy(iframe_src)
        pyautogui.hotkey('ctrl', 'v')
        pyautogui.press('enter')
        time.sleep(2) 
        #法院选择
        pyautogui.click(1250, 456)
        time.sleep(1)
        pyautogui.click(1250, 526)
        # 账户
        pyperclip.copy('zhaoyue2')
        #pyautogui.click(993, 346)
        pyautogui.click(1250, 546)
        time.sleep(1)
        pyautogui.hotkey('ctrl', 'v')
        # 密码
        pyperclip.copy('zhaoyue123456')
        #pyautogui.click(993, 346)
        pyautogui.click(1250, 616)
        time.sleep(1)
        pyautogui.hotkey('ctrl', 'v')
        time.sleep(1)
        driver.execute_script('document.getElementById("btnLoad").click();')
        time.sleep(6)
        # #法院选择
        # pyautogui.click(1067, 500)
        # time.sleep(1)
        # pyautogui.click(1058, 576)
        # time.sleep(1)
        #  # 账户zhaoyue123456
        # pyperclip.copy('zhaoyue2')
        # pyautogui.click(1091, 578)
        # time.sleep(1)
        # pyautogui.hotkey('ctrl', 'v')
        # # 密码
        # pyautogui.click(1089, 653)
        # pyperclip.copy('zhaoyue123456')
        # time.sleep(1)
        # pyautogui.hotkey('ctrl', 'v')
        # #driver.execute_script('document.getElementById("passwordfm3").value = "zhaoyue123456";')
        # # 点击登陆
        # time.sleep(1) 
        # driver.switch_to.default_content()
        # driver.execute_script('document.getElementById("btnLoad").click();')
        # time.sleep(1)
    except Exception:
        return None
# 通过时间 获取list
def request(driver):
    #点击查询
    pyautogui.click(1742, 559)
    time.sleep(1)
    pyautogui.click(1850, 1020)
    time.sleep(1)
    pyautogui.click(1850, 970)
    time.sleep(3)
    
    # 获取倒数第一个请求
    if driver.requests:
        last_request = driver.last_request
        if last_request.host == 'update.googleapis.com':
            last_request=driver.requests[-2]

        # 获取响应体的内容
        if last_request.response:
            response_body = last_request.response.body
            
            try:
                # 尝试解析JSON数据
                response_json = response_body.decode('utf-8')
                list_data = json.loads(response_json)
                return list_data
            except ValueError:
                print("Response body is not a valid JSON")
    else:
        print("No requests were made.")
# 解析列表json 并新增查询条件
def inquire(list_data):
    list_collection = list_data['data'][0]['listResult']['list']
    for item in list_collection:
        caseType=identify_case_type(item['cah']) 
        if caseType=='强制清算与破产':
            continue
        caseNumber=get_case_number(item['cah'])
        get_case(caseType,caseNumber)
        #点击查询
        pyautogui.click(1742, 559)
        time.sleep(2)
        #点击检查
        pyautogui.click(1845, 650)
        time.sleep(2)
        break

    
# 增加 案号与类型的查询条件
def get_case(caseType,caseNumber):
    # 点击案号
    pyperclip.copy(caseNumber)
    pyautogui.click(1278, 300)
    pyautogui.hotkey('ctrl', 'a')
    pyautogui.hotkey('ctrl', 'v')
    time.sleep(2)
    # 点击案件类型
    pyperclip.copy(caseType)
    pyautogui.click(1680, 300)
    pyautogui.hotkey('ctrl', 'a')
    pyautogui.hotkey('ctrl', 'v')
    time.sleep(2)
    pyautogui.click(1703, 353)
   
# 通过字典匹配汉字 获取案件类型
def identify_case_type(caseName):
    # 定义一个关键词与案件类型的映射字典
    case_keywords = {
        '民辖': '管辖案件',
        '刑': '刑事案件',
        '民': '民事案件',
        '行': '行政案件',
        '赔': '国家赔偿',
        '执': '执行类案件',
        '破': '强制清算与破产',
        '清': '强制清算与破产'
    }
    for keyword, case_type in case_keywords.items():
        if keyword in caseName:
            return case_type
    return '其他类型案件'
# 获取案号的顺序号
def get_case_number(text):
    # 给定的字符串
    # 正则表达式匹配号字前面的数字1
    match = re.search(r'(\d+)号', text)
    # 如果找到匹配，则提取数字
    if match:
        number_before_hao = match.group(1)  # 这将是'1'

        return number_before_hao
    else:
        print('没有找到匹配的数字。')
